package handler

import (
	"fmt"
	"choujiang/box_server/service"
	"choujiang/common/utils"
	"github.com/gin-gonic/gin"
	"choujiang/common/models"
	"choujiang/common/consts"
	"strconv"
)

func GroupList(c *gin.Context) {
	form := c.Request.Form
	uniqueId := form.Get("uniqueId")
	pageNo := form.Get("pageNo")
	pageSize := form.Get("pageSize")
	user := model.GetUserByUniqueId(uniqueId)
	if user == nil {
		utils.Error(consts.ErrUserNotFound, c)
		return
	}
	int64PageNo, _ := strconv.ParseInt(pageNo, 10, 64)
	int64PageSize, _ := strconv.ParseInt(pageSize, 10, 64)
	ret := make(map[string]interface{})
	groups, err := service.GetGroupList(user.Id, int64PageNo, int64PageSize)
	if err != nil {
		fmt.Printf("service.GroupList err:", err)
	}
	ret["data"] = groups
	todayAdd, totalAdd := service.GetGroupsNum(user.Id)
	ret["today_members"] = todayAdd
	ret["total_members"] = totalAdd
	utils.JSON(ret, c)
}
